package com.baosight.risk.common.quartz;

import com.baosight.risk.api.ridc.RidcRiskService;
import com.baosight.risk.common.utils.DateUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.quartz.SysJobParamMapper;
import com.baosight.risk.service.quartz.entity.SysJobParam;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author duanyongqiang
 * <p>
 * 定时任务方法名
 */
@Slf4j
@Component("ridcRiskTask")
public class RidcRiskTask {
    @Autowired
    private RidcRiskService ridcRiskService;

    @Autowired
    private SysJobParamMapper sysJobParamMapper;

    public void testParams(String params, String jobId) {
        try {

            log.error("进入定时任务————重点风险档案");
            String acctYear = "";
            String quarterCurrent = "";
            String manageCompCode = "";
            String manageCompName = "";
            String timingTaskId="";
            if (StringUtils.isNotEmpty(params) && params.indexOf(",") > 0) {
                String[] split = params.split(",");
                acctYear = split[0];
                quarterCurrent = split[1];
                manageCompCode = split[2];
                manageCompName = split[3];
                timingTaskId=split[4];
            }

            List<String> manageCompCodeList = new ArrayList<String>();
            List<String> manageCompNameList = new ArrayList<String>();
            List<XtzyCompany> companyList=new ArrayList<XtzyCompany>();
            if(StringUtils.isNotEmpty(manageCompCode)){
                String [] manageCompCodeData=manageCompCode.split("\\\\");
                String [] manageCompNameData=manageCompName.split("\\\\");
                manageCompCodeList= Arrays.asList(manageCompCodeData);
                manageCompNameList= Arrays.asList(manageCompNameData);
            }
            if(StringUtils.isEmpty(manageCompCode)){
                SysJobParam param=new SysJobParam();
                param.setJobGuid(timingTaskId);
                List<SysJobParam> jobParamList= sysJobParamMapper.select(param);
                if (StringUtils.isEmpty(manageCompCode) && jobParamList.size()>0) {
                    String [] manageCompCodeData=manageCompCode.split("\\\\");
                    manageCompCodeList= Arrays.asList(manageCompCodeData);;
                }
                if (StringUtils.isEmpty(manageCompName)) {
                    String [] manageCompNameData=manageCompName.split("\\\\");
                    manageCompNameList= Arrays.asList(manageCompNameData);
                }
            }
            if (StringUtils.isEmpty(acctYear)) {
                acctYear = DateUtils.format(new Date(), "yyyy");
            }
            if (StringUtils.isEmpty(quarterCurrent)) {
                quarterCurrent = DateUtils.getQuarter(new Date());
            }
            for (int i=0;i<manageCompCodeList.size();i++){
                ridcRiskService.addRidcRisk(params, jobId,manageCompCodeList.get(i),manageCompNameList.get(i));
            }

            log.error("执行结束————重点风险档案");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    public void testNoParams() {
        log.error("执行无参方法");
    }
}
